/*
 *  player_configure.c
 *  ICE_Player
 *
 *  Created by xl on 11-7-3.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#import <Foundation/Foundation.h>
#include "player_configure.h"

@interface CFG_FILE_XML : NSObject <NSXMLParserDelegate>
{
	ICE_CONFIG* p_cur_cfg;
}

- (BOOL)doXMLParse:(char*)xml_path cfg_structure:(ICE_CONFIG*)cfg;
- (BOOL)writeXML:(char*)xml_path cfg_structure:(ICE_CONFIG*)cfg;

@end

@implementation CFG_FILE_XML

- (id)init
{
	p_cur_cfg = NULL;
	return [super init];
}

- (BOOL)doXMLParse:(char*)xml_path cfg_structure:(ICE_CONFIG*)cfg
{
    //try to open file
    FILE* hf = fopen(xml_path, "rb");
    if (!hf)
    {
        return FALSE;
    }
    fclose(hf);
    
    NSString* path_str = [[NSString alloc]initWithCString:xml_path encoding:NSUTF8StringEncoding];
    NSData* data = [[NSData alloc] initWithContentsOfFile:path_str];
	NSXMLParser* parser = [[NSXMLParser alloc]initWithData:data];
	if (!parser)
	{
        [data release];
		[path_str release];
		return FALSE;
	}
	
	p_cur_cfg = cfg;
	
	[parser setDelegate:self];
	[parser setShouldProcessNamespaces:NO];
	[parser setShouldReportNamespacePrefixes:NO];
	[parser setShouldResolveExternalEntities:NO];
	[parser parse];
	
	NSError* parseError = [parser parserError];
	if (parseError)
	{
		NSLog(parseError.domain);
	}
	
	p_cur_cfg = NULL;
	
	[parser release];
	[data release];
    [path_str release];
	return TRUE;
}

- (BOOL)writeXML:(char*)xml_path cfg_structure:(ICE_CONFIG*)cfg
{
    if (!xml_path || !cfg)
    {
        return FALSE;
    }
	//NSString* head = @"<?xml version=\"1.0\" encoding=\"UTF-8\">\n\r\<Configure>\n\r<Configure Locked_Rotation=\"YES\">\n\r</Configure>\n";
    char* head = "<?xml version=\"1.0\" encoding=\"UTF-8\">\n\r\"";
    char content[CFG_MAX_STR_LEN] = {0};
    char* buf =  content;
    
    //write elements
    strcat(buf, head);
    sprintf(buf, "<%s>\n\r", CFG_SECTION_TAG);
    buf = &buf[strlen(buf)];
    
    if (cfg->is_rotation_locked)
    {
        sprintf(buf, "<%s %s=\"%s\">\n\r", CFG_SECTION_TAG, CFG_IS_ROTATION_LOCKED,CFG_YES);
    }
    else
    {
        sprintf(buf, "<%s %s=\"%s\">\n\r", CFG_SECTION_TAG, CFG_IS_ROTATION_LOCKED,CFG_NO);
    }
    buf = &buf[strlen(buf)];
    
    if (cfg->is_files_locked)
    {
        sprintf(buf, "<%s %s=\"%s\">\n\r", CFG_SECTION_TAG, CFG_IS_FILES_LOCKED,CFG_YES);
    }
    else
    {
        sprintf(buf, "<%s %s=\"%s\">\n\r", CFG_SECTION_TAG, CFG_IS_FILES_LOCKED,CFG_NO);
    }
    buf = &buf[strlen(buf)];
    sprintf(buf, "<%s %s=\"%s\">\n\r", CFG_SECTION_TAG, CFG_PASSWORD,cfg->pass_word);
    
    buf = &buf[strlen(buf)];
    
    if (cfg->enable_subtitle)
    {
        sprintf(buf, "<%s %s=\"%s\">\n\r", CFG_SECTION_TAG, CFG_ENABLE_SUBTITLE,CFG_YES);
    }
    else
    {
        sprintf(buf, "<%s %s=\"%s\">\n\r", CFG_SECTION_TAG, CFG_ENABLE_SUBTITLE,CFG_NO);
    }
    buf = &buf[strlen(buf)];
    
    if (cfg->enable_text_info)
    {
        sprintf(buf, "<%s %s=\"%s\">\n\r", CFG_SECTION_TAG, CFG_ENABLE_TEXT_INFO,CFG_YES);
    }
    else
    {
        sprintf(buf, "<%s %s=\"%s\">\n\r", CFG_SECTION_TAG, CFG_ENABLE_TEXT_INFO,CFG_NO);
    }
    buf = &buf[strlen(buf)];
    
    sprintf(buf, "<%s %s=\"%d\">\n\r", CFG_SECTION_TAG, CFG_LOOP_MODE,cfg->loop_mode);
    
    buf = &buf[strlen(buf)];
    sprintf(buf, "<\\%s>\n\r", CFG_SECTION_TAG);
    
    //write to file
    FILE* hf = fopen(xml_path, "wb");
    if (!hf)
    {
        return FALSE;
    }
    fwrite(content, 1, strlen(content), hf);
    fclose(hf);
    
	return TRUE;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	if (qName)
	{
		elementName = qName;
	}
	if ([elementName isEqualToString:@CFG_SECTION_TAG])
	{
        NSString* str = NULL;
        
        str = (NSString*)[attributeDict objectForKey:@CFG_IS_ROTATION_LOCKED]; 
		if (str && [str isEqualToString:@CFG_YES])
		{
			p_cur_cfg->is_rotation_locked = true;
		}
		else if(str && [str isEqualToString:@CFG_NO])
		{
			p_cur_cfg->is_rotation_locked = false;
		}
        
        str = (NSString*)[attributeDict objectForKey:@CFG_IS_FILES_LOCKED];
        if (str && [str isEqualToString:@CFG_YES])
		{
			p_cur_cfg->is_files_locked = true;
		}
		else if(str && [str isEqualToString:@CFG_NO])
		{
			p_cur_cfg->is_files_locked = false;
		}
        
        str = (NSString*)[attributeDict objectForKey:@CFG_PASSWORD];
        if (str)
        {
            [str getCString:p_cur_cfg->pass_word maxLength:CFG_MAX_PSW_LEN encoding:NSUTF8StringEncoding];
        }
        
        str = (NSString*)[attributeDict objectForKey:@CFG_ENABLE_SUBTITLE];
        if (str && [str isEqualToString:@CFG_YES])
		{
			p_cur_cfg->enable_subtitle = true;
		}
		else if(str && [str isEqualToString:@CFG_NO])
		{
			p_cur_cfg->enable_subtitle = false;
		}
        
        str = (NSString*)[attributeDict objectForKey:@CFG_ENABLE_TEXT_INFO];
        if (str && [str isEqualToString:@CFG_YES])
		{
			p_cur_cfg->enable_text_info = true;
		}
		else if(str && [str isEqualToString:@CFG_NO])
		{
			p_cur_cfg->enable_text_info = false;
		}
        
        if ([attributeDict objectForKey:@CFG_LOOP_MODE])
        {
            char a_mode[64] = {0};
            [(NSString*)[attributeDict objectForKey:@CFG_LOOP_MODE] getCString:a_mode maxLength:64 encoding:NSUTF8StringEncoding];
            p_cur_cfg->loop_mode = atoi(a_mode);
        }
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
	if (qName)
	{
		elementName = qName;
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	//get text string of elements <a>text</a>
}

@end



ICE_CONFIG* icfg_create_configure()
{
	ICE_CONFIG* ret_val = (ICE_CONFIG*)malloc(sizeof(ICE_CONFIG));
	if (ret_val)
	{
		ret_val->reserved = (void*)[[CFG_FILE_XML alloc] init];
	}
	return ret_val;
}

bool icfg_get_configure_from_file(ICE_CONFIG* p_cfg, char* path)
{
    if (!p_cfg)
	{
		return false;
	}
	CFG_FILE_XML* p_xml = (CFG_FILE_XML*)p_cfg->reserved;
    if ([p_xml doXMLParse:path cfg_structure:p_cfg])
    {
        return true;
    }
	return false;
}

bool icfg_put_configure_to_file(ICE_CONFIG* p_cfg, char* path)
{
    if (!p_cfg)
	{
		return false;
	}
	CFG_FILE_XML* p_xml = (CFG_FILE_XML*)p_cfg->reserved;
    if([p_xml writeXML:path cfg_structure:p_cfg])
    {
        return true;
    }
	return false;
}

void icfg_release_configure(ICE_CONFIG* p_cfg)
{
	if (!p_cfg)
	{
		return;
	}
	CFG_FILE_XML* p_xml = (CFG_FILE_XML*)p_cfg->reserved;
	[p_xml release];
	free(p_cfg);
}

static char* home_path = NULL;

char* icfg_get_app_home_path()
{
    if (!home_path)
    {
        home_path = malloc(CFG_MAX_PATH_LEN);
        [NSHomeDirectory() getCString:home_path];
    }
    return home_path;
}